package com.adamall.controller;

import com.adamall.dao.ShopMapper;
import com.adamall.module.Shop;
import com.adamall.module.ShopExample;
import com.adamall.module.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/v1")


public class Shops_Controller {
    @Autowired
    ShopMapper shopMapper;
    ShopExample shopExample;
    ShopExample.Criteria criteria;

    //    创建商铺
    @PostMapping("/shops")
    public ResponseEntity<Object> create(@RequestBody Shop shop) {
//        Shop shop1 = new Shop();
//        shop1.setName(shop.getName());
//        shop1.setDescription(shop.getDescription());
//        shop1.setImageurl(shop.getImageurl());
//        shop1.setOwnerid(getCurrentUser().getId());
        shop.setOwnerid(getCurrentUser().getId());
        shopMapper.insert(shop);
//  查询
        shopExample = new ShopExample();
        criteria = shopExample.createCriteria();
        criteria.andOwneridEqualTo(getCurrentUser().getId());
        List<Shop> shopList = shopMapper.selectByExample(shopExample);
        return ResponseEntity.status(200).header("Custom-Header", "创建商铺").body(Response_data.of(shopList.get(shopList.size() - 1)));
    }

    @GetMapping("/shops")
    public ResponseEntity<Object> PagingQueries(@RequestParam int pageNum, int pageSize, @CookieValue(value = "JSESSIONID") String cookie) {
        if (cookie.isEmpty()) {
            return ResponseEntity.status(400).header("Custom-Header", "不登录无法访问资源").body("不登录无法访问资源");
        }
        if (getCurrentUser() != null) {
            List<Shop> shopList = new ArrayList<>();
            shopExample = new ShopExample();
            criteria = shopExample.createCriteria();
            int all = shopMapper.selectByExample(shopExample).size();
            int index = pageNum * pageSize - pageSize + 1;
            for (int i = index; i < index + pageSize; i++) {
                if (i <= all) {
                    shopList.add(shopMapper.selectByPrimaryKey(index));
                }
            }

            return ResponseEntity.ok().header("Custom-Header", "Status code is 200").body(Response_data.of(shopList));
        }
        return ResponseEntity.status(400).header("Custom-Header", "登出后无法访问资源").body("登出后无法访问资源");
    }
    @DeleteMapping("shops/{id}")
    public ResponseEntity<String> del(@PathVariable int id){
        shopMapper.deleteByPrimaryKey(id);
        return ResponseEntity.status(200).header("Custom-Header", "Status code is 200").body("del");
    }

    //    获取指定id
    @GetMapping("shops/{id}")
    public ResponseEntity<Object> getId(@PathVariable int id) {
//        shopExample = new ShopExample();
//        criteria = new ShopExample.Criteria();
//        criteria.andOwneridEqualTo(getCurrentUser().getId()).andIdEqualTo(id);//查找当前用户的id有多少个店铺
//        List<Shop> shopList = shopMapper.selectByExample(shopExample);

        return ResponseEntity.status(200).header("Custom-Header", "可以访问刚刚创建的商铺").body(Response_data.of(shopMapper.selectByPrimaryKey(id)));
    }

    //获取用户
    public User getCurrentUser() {
        return (User) SecurityUtils.getSubject().getPrincipal();
    }

}
